package com.gitee.mrj.aiboot.core.sf.generators;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;

public interface FormDefinitionGenerator {

    /**
     * 字段上面的注解处理
     *
     * @param root   属性或上级节点对象
     * @param field  字段
     * @param uiForm 当前节点对象
     */
    default void generate(UIForm root, Field field, UIForm uiForm) {
    }

    /**
     * Class上面的注解处理
     *
     * @param uiForm 当前节点对象
     * @param clazz  类
     */
    default void generate(UIForm uiForm, Class clazz) {
    }


    default ObjectNode putUiGrid(ObjectNode ui) {
        return putObject(ui, "grid");
    }

    default ObjectNode putObject(ObjectNode ui, String field) {
        if (!ui.has(field)) {
            ui.putObject(field);
        }
        return (ObjectNode) ui.get(field);
    }

    default ArrayNode required(ObjectNode schema) {
        return putArray(schema, "required");
    }

    default ArrayNode putArray(ObjectNode schema, String fieldName) {
        if (!schema.has(fieldName)) {
            schema.putArray(fieldName);
        }
        ArrayNode required = (ArrayNode) schema.get(fieldName);
        return required;
    }

    default void putString(ObjectNode schema, String key, String value) {
        if (StringUtils.isNotBlank(value)) {
            schema.put(key, value);
        }
    }

    default void putInt(ObjectNode schema, String key, int value) {
        if (Integer.MIN_VALUE != value) {
            schema.put(key, value);
        }
    }

    default void putDouble(ObjectNode schema, String key, double value) {
        if (Double.MIN_VALUE != value) {
            schema.put(key, value);
        }
    }

    Class[] supports();
}
